package com.ray.server;
import java.io.OutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import com.ray.basic.BASIC;

public class TCPEchoServer{
	private static HashMap<String, OutputStream> map_out= new HashMap<String, OutputStream>();
	private static HashMap<String, Socket> map_socket= new HashMap<String, Socket>();
	private static HashMap<Integer, Server_in> map_read= new HashMap<Integer, Server_in>();
	private static Login mylog= new Login();
	public static void main(String [] args) throws Exception{
		ServerSocket servSock=new ServerSocket(BASIC.get_servPort());
		int clientid=1;// initial client id
		Thread write= new Thread(new Server_out());
		write.start();
		Thread udpin= new Thread(new Udp_in());
		udpin.start();
		while(true){
			Socket clntSock=servSock.accept();
			
			SocketAddress clientAddress=clntSock.getRemoteSocketAddress();
			System.out.println("Handling client at "+clientAddress);
			
			Server_in temp_read= new Server_in(clntSock, clientid);
			Thread read= new Thread(temp_read);
			read.start();
			map_read.put(clientid, temp_read);
			clientid++;
		}
	}
	public static HashMap<String, OutputStream> get_map_out(){
		return map_out;
	}
	public static HashMap<String, Socket> get_map_socket(){
		return map_socket;
	}
	public static Login get_mylog(){
		return mylog;
	}
	public static HashMap<Integer, Server_in> get_map_read(){
		return map_read;
	}
	public static void set_map_out(String username, OutputStream out){
		map_out.put(username, out);
	}
	public static void set_map_socket(String username, Socket clntSock){
		map_socket.put(username, clntSock);
	}
}